package com.axioma.dbmaint.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.axioma.dbmaint.model.Database;
import com.axioma.dbmaint.model.Server;
import com.axioma.dbmaint.service.ManagementService;

@Controller
public class ServersController {

   @Autowired
   ManagementService managementService;
   
	@RequestMapping("/serverList")
	public String spewServers(Model model){
	   List<Server> servers = managementService.getServers();
	   model.addAttribute("serverList", servers);
		return "serverList";
	}
	
	
	@RequestMapping("/serverDetails")
	public String editServer(@RequestParam("server")String serverName, Model model) {
	   model.addAttribute("server", serverName);
	   
	   List<Database> databases = managementService.getDatabases(serverName);
	   model.addAttribute("databaseList", databases);
	   return "serverDetails";
	}

	
	@RequestMapping(value="/servers", method=RequestMethod.GET, headers="Accept=application/json")
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody ServerList getRestServers() {
	   List<Server> servers = managementService.getServers();
	   return new ServerList(servers);
	}
	   

	
	class ServerList {
	   private List<Server> servers;
	   
	   public ServerList(List<Server> servers) {
	      this.servers = servers;
	   }
	}
}
